<?php

namespace app\core\helpers;

use app\core\entities\User;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use Yii;

class UserHelper
{
    public static function statusList(): array
    {
        return [
            User::STATUS_WAIT => 'wait',
            User::STATUS_ACTIVE => 'active',
        ];
    }

    /**
     * @throws \Exception
     */
    public static function statusName($status): string
    {
        return ArrayHelper::getValue(self::statusList(), $status);
    }

    public static function getSetting($key, $default = null)
    {
        $settings = Json::decode(Yii::$app->user->identity->user->settings, true);
        return $settings[$key] ?? $default;
    }

    public static function setSetting($key, $value): void
    {
        $settings = Json::decode(Yii::$app->user->identity->user->settings, true);
        $settings[$key] = $value;

        Yii::$app->user->identity->user->settings = json_encode($settings, JSON_UNESCAPED_UNICODE);

        $user = User::findOne(Yii::$app->user->id);

        if ($user) {
            $user->settings = Json::encode($settings);
            $user->save();
        }
    }

    public static function isModeSimple(): bool
    {
        return self::getSetting('mode', 0) == 1;
    }

    public static function isModeExtended(): bool
    {
        return self::getSetting('mode', 0) == 0;
    }
}
